import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
  state:{
    userInfo: null,
    isShow:false
  },
  getters:{
    getUserFn(state) {
      let userInfo = state.userInfo;
      if(!userInfo){
        userInfo = JSON.parse(localStorage.getItem('userInfo') || null)
      }
      return userInfo;
    },
    isShowMethod(state){
      return state.isShow;
    }

  },
  actions:{
    showSideBar({commit}){
      commit('showBar')
    },
    hideSideBar({commit}){
      commit('hideBar')
    }


    // Login(context, data) {
    //   axios.post('/api/login', {
    //     userName: data.username,
    //     pwd: data.password
    //   }).then((res) => {
    //       context.commit('setUserFn', res.data);
    //       window.localStorage.setItem('userInfo', JSON.stringify(res.data));
    //       return res;
    //     }).catch(function (error) {});
    // }
  },
  mutations:{
    setUserFn(state, data){
      state.userInfo = data.data;
      localStorage.setItem('userInfo', JSON.stringify(data.data));
    },
    loginOutFn(state){
      state.userInfo = null;
      localStorage.removeItem("userInfo");
    },
    showBar(state){
      state.isShow=true;
    },
    hideBar(state){
      state.isShow=false;
    }
  }
});
